<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

/**
 * Recursively strips slashes from all values in an array
 */
function stripslashesdeep($value)
{
  if (is_array($value)) {
    $return = array_map('stripslashesdeep', $value);
    return $return;
  }
  else {
    $return = stripslashes($value);
    return $return;
  }
}

/**
 * Recursively add slashes from all values in an array
 */
function addslashesdeep($value)
{
  if (is_array($value)) {
    $return = array_map('addslashesdeep', $value);
    return $return;
  }
  else {
    $return = addslashes($value);
    return $return;
  }
}

/**
 * 
 * @return unknown_type
 */
function getRealIpAddress()
{
  if (! empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
  }
  elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  else {
    $ip = $_SERVER['REMOTE_ADDR'];
  }
  return $ip;
}

/**
 * outputs an array/object as a preformated (HTML <pre></pre>) string.
 */
function pre()
{
  $args = func_get_args();
  reset($args);
  echo '<div style="text-align:left">';
  while (count($args)) {
    echo '<pre>';
    print_r(array_shift($args));
    echo '</pre>';
  }
  echo '</div>';
}

/**
 * if $param is a valid key in $source, return it's value
 * otherwise return $default, $source can be eather an array or an object
 */
function getParam($source, $param, $default = null, $testEmpty = true)
{
  if ($testEmpty) return ! empty($source[$param]) ? $source[$param] : $default;
  
  return isset($source[$param]) ? $source[$param] : $default;
}

/**
 *
 */
function url($url, $traillingSlash = false)
{
  $url = preg_replace('#\\\+#', '/', $url);
  
  if (! preg_match('|\.[^/]+$|', $url)) {
    if ($traillingSlash) {
      if (! strrpos('/', $url) === 0) $url .= '/';
    }
    elseif (strrpos('/', $url) === 0) {
      $url = substr($url, 0, strlen($url) - 1);
    }
  }
  
  return $url;
}

/**
 *
 */
function http($url, $protocol = 'http')
{
  preg_match('#^[a-z]+:/+(.*)#', $url, $o);
  $url = $protocol . '://' . getParam($o, 1);
  return $url;
}

/**
 *
 */
function strip_http($url)
{
  preg_match('#^[a-z]+://(.*)#i', $url, $o);
  $url = $o[1];
  return $url;
}

/**
 *
 */
function define_once($name, $value)
{
  if (! defined($name)) {
    define($name, $value);
  }
}

/**
 *
 */
function path_is_absolute($path)
{
  return IS_WIN ? preg_match('#^([a-z]:.+)$#i', $path) : @file_exists($path);
}

/**
 *
 */
function url_is_absolute($url)
{
  return preg_match('|^https?://|i', $url);
}

/**
 *
 */
function sy_fix_path($path, $extension = null)
{
  // remove trailling slashes, duplicate slashes
  $find = array(
    '#\\\#', '#/+$#', '#/+#', '#/+$#' 
  );
  $replace = array(
    '/', '/', '/', '' 
  );
  $path = preg_replace($find, $replace, $path);
  
  // add missing extension
  if (! empty($extension) && ! preg_match('#\.' . $extension . '$#', $path)) {
    $path .= '.' . $extension;
  }
  
  return $path;
}

/**
 *
 */
function sy_fix_url($url)
{
  // remove trailling slashes
  $url = preg_replace('#/+$#', '', $url);
  
  return $url;
}

/**
 *
 */
function sy_fix_extension($path, $ext)
{
  if (! preg_match('/\.' . addslashes($ext) . '$/i', $path)) {
    $path .= '.' . $ext;
  }
  
  return $path;
}

/**
 *
 */
function sy_assertion_handler($file, $line, $code)
{
  throw new AssertionException('Assertion failed');
}

?>